*
* The Two-Way Effects of Populism on Affective Polarization
* Comparative Political Studies
* Braeden Davis, Jay Goodliffe, Kirk Hawkins
* Replication Code
*

* need to install outreg2 and parmest.
* uncomment and install, if necessary:
* ssc install outreg2
* ssc install parmest

* pickone == 1 picks one observation per country-election
* pickonepartyyear = 1 picks one observation per party in each country-election
* good == 1 establishes the working data set (Affective Polarization Weighted Distance): 
* 	no missing data on variables of Model 1
*	not a problematic election, which we exclude, see code at appendix 
*	favorite party not missing
*	non-independent (identifies close or closer party)
* good_uws = 1 establishes working data set for Affective Polarization Unweighted Distance

* mny_variable (e.g., mny_par_pop) is the mean of a variable, for each country-election
* dvy_variable (e.g., dvy_par_pop) is the de-meaned variable, by country-election

*
* Which elections are we dropping and why?
* (IMD1004 variable: Country Year)
*
* No lower house elections, and thus no V-Dem party scores:
* BLR_2001: Belarus 2001
* CHL_1999: Chile 1999
* FRA_2002: France 2002
* JPN_2004: Japan 2004
* JPN_2007: Japan 2007
* JPN_2013: Japan 2013
* KGZ_2005: Kyrgyzstan 2005
* LTU_1997: Lituhania 1997
* ROU_2009: Romania 2009
* ROU_2014: Romania 2014
* RUS_2000: Russia 2000
* RUS_2004: Russia 2004

* No V-DEM party scores:
* BLR_2008: Belarus 2008
* ISR_2020: Israel 2020
* NZL_2020: New Zealand 2020
* SVK_2020: Slovakia 2020
* TWN_2020: Taiwan 2020
* USA_2020: USA 2020

* No party ratings by respondents in CSES:
* CHE_2019: Switzerland 2019

* No close party asked in CSES:
* IRL_2007: Ireland 2007
* TUN_2019: Tunisia 2019

* ARG_2015 (Argentian 2015): For the Peronistas, CSES asks respondents to rate both the Justicialist Party (the Peronista party) and the Front for Victory, which Wikipedia describes as "a centre-left Peronist electoral alliance in Argentina, and is formally a faction of the Justicialist Party." The Front for Victory is the #1 Close Party, and the Justicialist Party is the #2 Close Party. So when people say they are close to one of those parties, then do we include the other in their affective polarization rating? That seems odd. (The ratings are correlated at 0.5.)
* BELF1999, BELW1999, BELF2019, BELW2019: Belgium 1999 and 2019 separated Flemish and Walloon, but are in the same system
* NOR_2001 (Norway 2001): not one respondent chose the third most popular party (Progress) as their closest party, which was the nationalist party (and the most populist party). Plenty of respondents chose it in 1997 and 2005 (and 2009 and 2013 and 2017). Seems like CSES mistake
* ROU_2004 (Romania 2004): CSES has respondents that pick both alliances as favorite and parties within the alliance as favorite. But CSES only has respondents rate the parties. We would have to create a respondent's rating of the alliance from the parties


* Unusual Election Coding:
* Ireland 2011 (IRL_2011) coded unusually. Here was their scheme:
* Ask if you are close to any party (CSES variable: IMD3005_1). 
* If yes, name the party (IMD3005_3), BUT NOT how close (IMD3005_4)
* If no, ask if any party is closer (IMD3005_2).
* If yes, DO NOT name the party (IMD3005_3), but ask how close (IMD3005_4)
* We need the name of the party to get party_pop, so only those that answered yes to the first close question are included.
* But we do not have close for any of them. List as close_noask for those.


* put the right working directory here
* cd "your working directory"

log using "DGH_populism_polar", replace

*
* Main Text
*

* Figure 1
* Average Affective Polarization (Weighted Distance) of included country-elections
* Ordered by average across elections
use "DGH_populism_polar.dta", clear
egen mny_new_polar_w_mean = mean(mny_new_polar_w) if pickone & good, by(country_name)
egen pickcountry = tag(mny_new_polar_w_mean country_name)
tab pickcountry, m
sum mny_new_polar_w_mean if pickcountry
sort mny_new_polar_w_mean
keep if pickcountry
gen mny_new_polar_w_mean_rank = _n 
keep countrynum mny_new_polar_w_mean_rank
save "countryrankpolarization.dta", replace
use "DGH_populism_polar.dta", clear
preserve
capture drop _merge
keep if pickone & good
sort countrynum
merge m:1 countrynum using "countryrankpolarization.dta"
sort countrynum mny_new_polar_w
by countrynum (mny_new_polar_w): replace country_name = "" if _n != _N
scatter mny_new_polar_w_mean_rank mny_new_polar_w, ///
	msize(small) ///
	mcolor(black) ///
	mlabel(country_name) ///
	mlabsize(vsmall) ///
	mlabcolor(gs8) ///
	ytitle("") ///
	yscale(range(0 52)) ///
	ylabel(, noticks nolabels nogrid) ///
	xscale(range(2 8)) ///
	xlabel(2(1)8, labsize(small) nogrid) ///
	xtitle("Average Country-Election Affective Polarization", size(small)) ///
	ysize(8.5) xsize(6.5) ///
	graphregion(color(white)) ///
	plotregion(lcolor(black))
graph save "Figure_1", replace
graph export "Figure_1.pdf", replace
restore


* Table 1
* main model: weighted spread
use "DGH_populism_polar.dta", clear
mixed new_polar_w dvy_par_pop dev_mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
sum countrynum if e(sample)
local N_c = r(max)
outreg2 using table1, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster replace

* main model: unweighted spread
mixed new_polar_uw dvyuws_par_pop dev_mnyuws_par_pop dvyuws_ideo_polar_m mnyuws_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good_uws || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
sum countrynum if e(sample)
local N_c = r(max)
outreg2 using table1, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append

* main model: weighted standard deviation
mixed a_polar dvy_par_pop dev_mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
sum countrynum if e(sample)
local N_c = r(max)
outreg2 using table1, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append

* interactive model
mixed new_polar_w c.dvy_par_pop##c.dev_mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,16]))^2
local var_i = (exp(e(b)[1,17]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
sum countrynum
local N_c = r(max)
outreg2 using table1, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll',  AIC, `AIC', BIC, `BIC') 2aster append text word excel


* Figure 2
* substantive effects
use "DGH_populism_polar.dta", clear
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
xtset countryyear
xtsum new_polar_w if good
xtsum dvy_par_pop if good
scalar dvy_par_pop_sd = r(sd)
xtsum mny_par_pop if good
scalar mny_par_pop_sd = r(sd)
xtsum dvy_ideo_polar_m if good
scalar dvy_ideo_polar_m_sd = r(sd)
xtsum mny_ideo_polar_m if good 
scalar mny_ideo_polar_m_sd = r(sd)
xtsum close_m if good 
scalar close_m_sd = r(sd)
xtsum gini if good 
scalar gini_sd = r(sd)
xtsum unemploy if good 
scalar unemploy_sd = r(sd)

* no statistical difference in substantive effect size of ideology and country populism
nlcom (country_effects: 2*mny_par_pop_sd*_b[mny_par_pop] - 2*mny_ideo_polar_m_sd*_b[mny_ideo_polar_m])
* no statistical difference in substantive effect size of unemployment and country populism
nlcom (country_effects: 2*unemploy_sd*_b[unemploy] - 2*mny_ideo_polar_m_sd*_b[mny_ideo_polar_m])
* no statistical difference in substantive effect size of ideology and unemployment
nlcom (country_effects: 2*mny_par_pop_sd*_b[mny_par_pop] - 2*unemploy_sd*_b[unemploy])

* all the effects
nlcom ///
	(dvy_par_pop: 2*dvy_par_pop_sd*_b[dvy_par_pop]) ///
	(mny_par_pop: 2*mny_par_pop_sd*_b[mny_par_pop]) ///
	(close_m: 2*close_m_sd*_b[close_m]) ///
	(dvy_ideo_polar_m: 2*dvy_ideo_polar_m_sd*_b[dvy_ideo_polar_m]) ///
	(mny_ideo_polar_m: 2*mny_ideo_polar_m_sd*_b[mny_ideo_polar_m]) ///
	(unemploy: 2*unemploy_sd*_b[unemploy]) ///
	(female: _b[female]) ///
	(PR1: _b[PR1]) ///
	(gini: 2*gini_sd*_b[gini]) ///	
	, post
parmest, format(estimate min95 max95 %8.4f) saving("substeffects.dta", replace)

use "substeffects.dta", clear
egen varnum = fill(10 9)
replace varnum = varnum - 1 in 3/L
twoway (rspike min95 max95 varnum, horizontal) ///
	(scatter varnum estimate, mcolor(dknavy) msize(vsmall)), ///
	ylabel(1 "Inequality" 2 "Proportional Representation" 3 "Female" 4 "Unemployment Rate" ///
	5 "Country-Election Ideological Polarization" 6 "Individual Ideological Polarization" ///
	7 "Individual Party Closeness" 8 " " 9 "Country-Election Populism" 10 "Individual Party Populism", angle(horizontal) labsize(medium)) ///
	title("") ///
	ytitle("") ///
	xline(0, lcolor(dknavy) lpattern(dot))  ///
	legend(off) ///
	ytick(1/7 9/10) ///
	xlabel(, format(%8.2f) nogrid) ///
	ylabel(, glpattern(solid)) ///
	xsize(6.5) ///
	graphregion(color(white))
graph save "Figure_2", replace
graph export "Figure_2.pdf", replace


* Penultimate paragraph of Results Section: 
* For what values of individual-party populism is the effect of country-election populism positive?
* marginal effects plot
use "DGH_populism_polar.dta", clear
mixed new_polar_w c.dvy_par_pop##c.mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
* get observed range of dvy_par_pop
sum dvy_par_pop mny_par_pop if e(sample)
margins, dydx(mny_par_pop) at(dvy_par_pop = (-.65(.1).75))
marginsplot, recast(line) recastci(rarea) 
* At what percentile of dvy_par_pop is the marginal effect of mny_par_pop 0?
* From the plot, where dvy_par_pop = -.61
_pctile dvy_par_pop if good, p(.01 .011 .012 .013 .014 .015 .016 .017 .018 .019 .02)
return list
di 100 - 0.017
* the marginal effect is positive for 99.983% of observations

* At what percentile of dvy_par_pop is the marginal effect of mny_par_pop 
* statisically different than 0?
* From the plot, where dvy_par_pop = -.3
_pctile dvy_par_pop if good, p(3.53 3.531 3.532 3.533 3.534 3.535 3.536 3.537 3.538 3.539 3.54)
return list
di 100-3.534
* the marginal effect is positive and statistically significant for 96.466% of observations


* Figure 3
* interactive model graphics
use "DGH_populism_polar.dta", clear
capture drop where
gen where = 3.615
capture drop pipe
gen pipe = "|"
mixed new_polar_w c.dvy_par_pop##c.mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
sum dvy_par_pop mny_par_pop if e(sample)
margins, at(dvy_par_pop = (-.376 .376) mny_par_pop = (.09(.01).81))
marginsplot, x(mny_par_pop) recast(line) recastci(rarea) ///
	legend(off) ///
	graphregion(color(white)) ///
	ciopts(fintensity(inten30)) ///
	xtitle("Country-Election Populism") ///
	xscale(range(0.09 0.81)) ///
	xlabel(.1(.1).7, nogrid) ///
	ylabel(, glpattern(solid)) ///
	yscale(range(3.5 6.2)) ///
	ytitle("Predicted Affective Polarization (Weighted Distance)") ///
	title("") ///
	text(4.13 .57 "low Individual Party Populism (-2 sd)", color(navy)) ///
	text(5.35 .35 "high Individual Party Populism (+2 sd)", color(maroon)) ///
	addplot(scatter where mny_par_pop if pickone, xscale(range(0.07 0.78)) ///
		xlabel(.1(.1).7) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off))
graph save "Figure_3", replace
graph export "Figure_3.pdf", replace


* Figure 4
* CSES Module 5 respondent populism interactive model graphics
use "DGH_populism_polar.dta", clear
capture drop where
gen where = 3.13
capture drop pipe
gen pipe = "|"
mixed new_polar_w c.dvy_ind_pop_cses##c.mny_ind_pop_cses dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing unemploy gini PR1 female if good || countryyear:
sum dvy_ind_pop_cses mny_ind_pop_cses if e(sample)
margins, at(dvy_ind_pop_cses = (-.567 .567) mny_ind_pop_cses = (.26(.01).73))
marginsplot, x(mny_ind_pop_cses) recast(line) recastci(rarea) ///
	legend(off) ///
	graphregion(color(white)) ///
	ciopts(fintensity(inten30)) ///
	xtitle("Citizenry Populism") ///
	xscale(range(.26 .73)) ///
	xlabel(.3(.1).7, nogrid) ///
	ylabel(, glpattern(solid)) ///
	ytitle("Predicted Affective Polarization (Weighted Distance)") ///
	title("") ///
	text(4 .52 "low Individual Populism (-2 sd)", color(navy)) ///
	text(5.5 .53 "high Individual Populism (+2 sd)", color(maroon)) ///
	addplot(scatter where mny_ind_pop_cses if pickone & e(sample), xscale(range(0.26 0.73)) ///
		xlabel(.3(.1).7) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off))
graph save "Figure_4", replace
graph export "Figure_4.pdf", replace


* Figure 5
* Models shown in Tables A-3(b) and A-3(c) run here to create the Figure.
* Also run models from Table A-3(a) here for completeness.
* Calculating and graphing substantive effects
* radical right Tables A-3 in appendix which creates Figure 5 for main text
* split into Northern/Western Europe and all other regions
use "DGH_populism_polar.dta", clear

* first, populism without rr
* all regions
gen goodrr = (good == 1 & !missing(rr))
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if goodrr || countryyear:
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_pop_all = group(country_name) if e(sample)
sum countrynum_pop_all
local N_c = r(max)
outreg2 using apptable3all, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster replace

* Northern/Western Europe
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if goodrr & (UNRegion == 154 | UNRegion == 155) || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_pop_nweur = group(country_name) if e(sample)
sum countrynum_pop_nweur
local N_c = r(max)
outreg2 using apptable3nweur, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster replace
capture drop goodrrnweur
gen goodrrnweur = e(sample)
xtset countryyear
xtsum new_polar_w if goodrrnweur
xtsum dvy_par_pop if goodrrnweur
scalar dvy_par_pop_sd = r(sd)
xtsum mny_par_pop if goodrrnweur
scalar mny_par_pop_sd = r(sd)
* all the effects
nlcom ///
	(dvy_par_pop: 2*dvy_par_pop_sd*_b[dvy_par_pop]) ///
	(mny_par_pop: 2*mny_par_pop_sd*_b[mny_par_pop])	///
	, post
parmest, format(estimate min95 max95 %8.4f) saving("substeffects_popnorr_nweur.dta", replace)

* other regions
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if goodrr & (UNRegion != 154 & UNRegion != 155) || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_pop_else = group(country_name) if e(sample)
sum countrynum_pop_else
local N_c = r(max)
outreg2 using apptable3else, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster replace
capture drop goodrrelse
gen goodrrelse = e(sample)
xtset countryyear
xtsum new_polar_w if goodrrelse
xtsum dvy_par_pop if goodrrelse
scalar dvy_par_pop_sd = r(sd)
xtsum mny_par_pop if goodrrelse
scalar mny_par_pop_sd = r(sd)
* all the effects
nlcom ///
	(dvy_par_pop: 2*dvy_par_pop_sd*_b[dvy_par_pop]) ///
	(mny_par_pop: 2*mny_par_pop_sd*_b[mny_par_pop])	///
	, post
parmest, format(estimate min95 max95 %8.4f) saving("substeffects_popnorr_else.dta", replace)

* second, rr without populism
* all regions
mixed new_polar_w dvy_rr mny_rr dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if goodrr || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_rr_all = group(country_name) if e(sample)
sum countrynum_rr_all
local N_c = r(max)
outreg2 using apptable3all, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append

* Northern/Western Europe
mixed new_polar_w dvy_rr mny_rr dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if goodrr & (UNRegion == 154 | UNRegion == 155) || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_rr_nweur = group(country_name) if e(sample)
sum countrynum_rr_nweur
local N_c = r(max)
outreg2 using apptable3nweur, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append
xtset countryyear
xtsum new_polar_w if goodrrnweur
xtsum dvy_rr if goodrrnweur
scalar dvy_rr_sd = r(sd)
xtsum mny_rr if goodrrnweur
scalar mny_rr_sd = r(sd)
* all the effects
nlcom ///
	(dvy_rr: 2*dvy_rr_sd*_b[dvy_rr]) ///
	(mny_rr: 2*mny_rr_sd*_b[mny_rr])	///
	, post
parmest, format(estimate min95 max95 %8.4f) saving("substeffects_rrnopop_nweur.dta", replace)

* other regions
mixed new_polar_w dvy_rr mny_rr dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if goodrr & (UNRegion != 154 & UNRegion != 155) || countryyear:
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_rr_else = group(country_name) if e(sample)
sum countrynum_rr_else
local N_c = r(max)
outreg2 using apptable3else, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append 
xtset countryyear
xtsum new_polar_w if goodrrelse
xtsum dvy_rr if goodrrelse
scalar dvy_rr_sd = r(sd)
xtsum mny_rr if goodrrelse
scalar mny_rr_sd = r(sd)
* all the effects
nlcom ///
	(dvy_rr: 2*dvy_rr_sd*_b[dvy_rr]) ///
	(mny_rr: 2*mny_rr_sd*_b[mny_rr])	///
	, post
parmest, format(estimate min95 max95 %8.4f) saving("substeffects_rrnopop_else.dta", replace)

* third put in populism and rr 
* all regions
mixed new_polar_w dvy_par_pop mny_par_pop dvy_rr mny_rr dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if goodrr || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,17]))^2
local var_i = (exp(e(b)[1,18]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_poprr_all = group(country_name) if e(sample)
sum countrynum_poprr_all
local N_c = r(max)
outreg2 using apptable3all, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append text word excel

* Northern/Western Europe
mixed new_polar_w dvy_par_pop mny_par_pop dvy_rr mny_rr dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if goodrr & (UNRegion == 154 | UNRegion == 155) || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,17]))^2
local var_i = (exp(e(b)[1,18]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_poprr_nweur = group(country_name) if e(sample)
sum countrynum_poprr_nweur
local N_c = r(max)
outreg2 using apptable3nweur, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append text word excel
* both of these variables are individually insignificant. are they jointly significant?
test mny_par_pop mny_rr
* significant
xtset countryyear
xtsum new_polar_w if goodrrnweur
xtsum dvy_par_pop if goodrrnweur
scalar dvy_par_pop_sd = r(sd)
xtsum mny_par_pop if goodrrnweur
scalar mny_par_pop_sd = r(sd)
xtsum dvy_rr if goodrrnweur
scalar dvy_rr_sd = r(sd)
xtsum mny_rr if goodrrnweur
scalar mny_rr_sd = r(sd)
* all the effects
nlcom ///
	(dvy_par_pop: 2*dvy_par_pop_sd*_b[dvy_par_pop]) ///
	(mny_par_pop: 2*mny_par_pop_sd*_b[mny_par_pop])	///
	(dvy_rr: 2*dvy_rr_sd*_b[dvy_rr]) ///
	(mny_rr: 2*mny_rr_sd*_b[mny_rr]) ///
	, post
parmest, format(estimate min95 max95 %8.4f) saving("substeffects_poprr_nweur.dta", replace)

* other regions
mixed new_polar_w dvy_par_pop mny_par_pop dvy_rr mny_rr dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if goodrr & (UNRegion != 154 & UNRegion != 155) || countryyear: 
local ll = e(ll)
local var_g = (exp(e(b)[1,17]))^2
local var_i = (exp(e(b)[1,18]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_poprr_else = group(country_name) if e(sample)
sum countrynum_poprr_else
local N_c = r(max)
outreg2 using apptable3else, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append text word excel
xtset countryyear
xtsum new_polar_w if goodrrelse
xtsum dvy_par_pop if goodrrelse
scalar dvy_par_pop_sd = r(sd)
xtsum mny_par_pop if goodrrelse
scalar mny_par_pop_sd = r(sd)
xtsum dvy_rr if goodrrelse
scalar dvy_rr_sd = r(sd)
xtsum mny_rr if goodrrelse
scalar mny_rr_sd = r(sd)
* all the effects
nlcom ///
	(dvy_par_pop: 2*dvy_par_pop_sd*_b[dvy_par_pop]) ///
	(mny_par_pop: 2*mny_par_pop_sd*_b[mny_par_pop])	///
	(dvy_rr: 2*dvy_rr_sd*_b[dvy_rr]) ///
	(mny_rr: 2*mny_rr_sd*_b[mny_rr]) ///
	, post
parmest, format(estimate min95 max95 %8.4f) saving("substeffects_poprr_else.dta", replace)

use "substeffects_popnorr_nweur.dta", clear
gen popnorr = 1
gen rrnopop = 0
gen poprr = 0
gen nweur = 1
save "substeffects_popnorr_nweur_update.dta", replace

use "substeffects_popnorr_else.dta", clear
gen popnorr = 1
gen rrnopop = 0
gen poprr = 0
gen nweur = 0
save "substeffects_popnorr_else_update.dta", replace

use "substeffects_rrnopop_nweur.dta", clear
gen popnorr = 0
gen rrnopop = 1
gen poprr = 0
gen nweur = 1
save "substeffects_rrnopop_nweur_update.dta", replace

use "substeffects_rrnopop_else.dta", clear
gen popnorr = 0
gen rrnopop = 1
gen poprr = 0
gen nweur = 0
save "substeffects_rrnopop_else_update.dta", replace

use "substeffects_poprr_nweur.dta", clear
gen popnorr = 0
gen rrnopop = 0
gen poprr = 1
gen nweur = 1
save "substeffects_poprr_nweur_update.dta", replace

use "substeffects_poprr_else.dta", clear
gen popnorr = 0
gen rrnopop = 0
gen poprr = 1
gen nweur = 0
save "substeffects_poprr_else_update.dta", replace

use "substeffects_popnorr_nweur_update.dta", clear
append using "substeffects_popnorr_else_update.dta"
append using "substeffects_rrnopop_nweur_update.dta"
append using "substeffects_rrnopop_else_update.dta"
append using "substeffects_poprr_nweur_update.dta"
append using "substeffects_poprr_else_update.dta"
gen targetnum = 5 if parm == "dvy_par_pop"
replace targetnum = 4 if parm == "mny_par_pop"
replace targetnum = 2 if parm == "dvy_rr"
replace targetnum = 1 if parm == "mny_rr"
replace targetnum = targetnum + .1 if nweur == 1
replace targetnum = targetnum - .1 if nweur == 0

twoway (rspike min95 max95 targetnum if popnorr == 1 & nweur == 1, horizontal lcolor(blue) lwidth(vthin)) ///
	(rspike min95 max95 targetnum if popnorr == 1 & nweur == 0, horizontal lcolor(green) lwidth(vthin)) ///
	(scatter targetnum estimate if popnorr == 1 & nweur == 1, msymbol(O) mcolor(blue) msize(vsmall)) ///
	(scatter targetnum estimate if popnorr == 1 & nweur == 0, msymbol(O) mcolor(green) msize(vsmall)), ///
	ylabel(1 "Country-Election Radical Right" 2 "Individual Radical Right"   ///
	4 "Country-Election Populism" 5 "Individual Party Populism", nogrid angle(horizontal) labsize(medium)) ///
	title("") ///
	subtitle("Populism" "No Radical Right") ///
	ytitle("") ///
	xline(0, lcolor(dknavy) lpattern(dot))  ///
	legend(off) ///
	xlabel(-0.3(0.3)0.6, format(%8.1f) nogrid) ///
	text(4.27 0.3 "N & W Europe", color(blue)) ///
	text(3.79 0.05 "Elsewhere", color(green)) ///
	graphregion(color(white)) ///
	fxsize(88.5)
graph save "substeffects_popnorr", replace

twoway (rspike min95 max95 targetnum if rrnopop == 1 & nweur == 1, horizontal lcolor(blue) lwidth(vthin)) ///
	(rspike min95 max95 targetnum if rrnopop == 1 & nweur == 0, horizontal lcolor(green) lwidth(vthin)) ///
	(scatter targetnum estimate if rrnopop == 1 & nweur == 1, msymbol(O) mcolor(blue) msize(vsmall)) ///
	(scatter targetnum estimate if rrnopop == 1 & nweur == 0, msymbol(O) mcolor(green) msize(vsmall)), ///
	ylabel(1 2 4 5, nolabels noticks nogrid) ///
	title("") ///
	subtitle("Radical Right" "No Populism") ///
	ytitle("") ///
	xline(0, lcolor(dknavy) lpattern(dot))  ///
	legend(off) ///
	yscale(lstyle(none)) ///
	xlabel(-0.3(0.3)0.6, format(%8.1f) nogrid) ///
	graphregion(color(white))
graph save "substeffects_rrnopop", replace

twoway (rspike min95 max95 targetnum if poprr == 1 & nweur == 1, horizontal lcolor(blue) lwidth(vthin)) ///
	(rspike min95 max95 targetnum if poprr == 1 & nweur == 0, horizontal lcolor(green) lwidth(vthin)) ///
	(scatter targetnum estimate if poprr == 1 & nweur == 1, msymbol(O) mcolor(blue) msize(vsmall)) ///
	(scatter targetnum estimate if poprr == 1 & nweur == 0, msymbol(O) mcolor(green) msize(vsmall)), ///
	ylabel(1 2 4 5, nolabels noticks nogrid) ///
	title("") ///
	subtitle("Populism and" "Radical Right") ///
	ytitle("") ///
	xline(0, lcolor(dknavy) lpattern(dot))  ///
	legend(off) ///
	yscale(lstyle(none)) ///
	xlabel(-0.3(0.3)0.6, format(%8.1f) nogrid) ///
	graphregion(color(white))
graph save "substeffects_poprr", replace

graph combine substeffects_popnorr.gph substeffects_rrnopop.gph substeffects_poprr.gph, ///
	rows(1) ///
	xsize(6.5) ///
	ysize(4) ///
	xcommon ///
	ycommon ///
	iscale(*1.3) ///
	graphregion(color(white)) ///
	imargin(1 1 1 1)
graph save "substeffects_poprrall", replace
graph export "substeffects_poprrall.pdf", as(pdf) replace



*
* Results noted in main text, but not shown
*

* fn 4: Results stronger if we drop those who rate their close party less than average
* how many observations are below 0 (close < avg)?
use "DGH_populism_polar.dta", clear
count if new_polar_w < -1e-05 & good 
* 5,113: 
di 5113/155379
* 3.3%
* Dropping those respondents (assuming a mistake) where polarization < 0
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good & new_polar_w > -1e-05 || countryyear: 
* even stronger for party_pop mny_par_pop. gini no longer significant
* Using absolute value for those respondents (assuming a particular mistake)
gen abs_new_polar_w = abs(new_polar_w)
mixed abs_new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
* very similar to excluding < 0


* fn 7: Other methods of combining the two measures for populism: 
use "DGH_populism_polar.dta", clear
* arithmetic mean
mixed new_polar_w dvy_a_par_pop mny_a_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
* geometric mean
mixed new_polar_w dvy_g_par_pop mny_g_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
* minimum
mixed new_polar_w dvy_l_par_pop mny_l_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear:
* maximum
mixed new_polar_w dvy_h_par_pop mny_h_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 


* fn 8 and fn 11: dropping those with missing/not asked Ideology and those with missing/not asked Closeness
use "DGH_populism_polar.dta", clear
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar mny_ideo_polar dvy_close mny_close unemploy gini PR1 female if good || countryyear: 


* fn 10: party Closeness does not have a separate effect at country-election level
use "DGH_populism_polar.dta", clear
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask dvy_close_m mny_close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
test dvy_close_m = mny_close_m
* no statistical difference


* last paragraph of control variables: Days Since Election
use "DGH_populism_polar.dta", clear
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female days if good || countryyear:


* fn 16: Using vote shares as the weight on country-election populism produces similar results
use "DGH_populism_polar.dta", clear
mixed new_polar_w dvy_par_pop country_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 
corr mny_par_pop country_pop if pickone & good
* correlated at .93


* fn 17: Using sampling, demographic, and political weights
use "DGH_populism_polar.dta", clear
* survey weights
mixed new_polar_w dvywts_par_pop mnywts_par_pop dvywts_ideo_polar_m mnywts_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good [pweight = IMD1010_1] || countryyear: 
* demographic weights
mixed new_polar_w dvywtd_par_pop mnywtd_par_pop dvywtd_ideo_polar_m mnywtd_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good [pweight = IMD1010_2] || countryyear: 
* political weights
mixed new_polar_w dvywtp_par_pop mnywtp_par_pop dvywtp_ideo_polar_m mnywtp_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good [pweight = IMD1010_3] || countryyear: 
* all similar results


* Second paragraph of Results: Influential outliers?
use "DGH_populism_polar.dta", clear
* Dropping one election at a time
matrix E = J(185,4,0)
forvalues i = 1/185 {
	quietly mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good & countryyear != `i' || countryyear: 
	matrix E[`i',1] = r(table)[1,1]
	matrix E[`i',2] = r(table)[4,1]
	matrix E[`i',3] = r(table)[1,2]
	matrix E[`i',4] = r(table)[4,2]
}
matrix list E
* Coefficients for Individual-Party Populism range from .475 (dropping Brazil 2002) 
* to .530 (dropping Australia 2013), all highly significant
* Coefficients for Country-Election Populism range from 1.37 (dropping Hungary 2018) 
* to 1.61 (dropping Philippines 2010), all significant

* Dropping one country at a time
matrix C = J(53,4,0)
forvalues i = 1/53 {
	quietly mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing 		 close_noask unemploy gini PR1 female if good & countrynum != `i' || countryyear: 
	matrix C[`i',1] = r(table)[1,1]
	matrix C[`i',2] = r(table)[4,1]
	matrix C[`i',3] = r(table)[1,2]
	matrix C[`i',4] = r(table)[4,2]
}
matrix list C
* Coefficients for Individual-Party Populism range from .449 (dropping Uruguay) 
* to .557 (dropping New Zealand), all highly significant
* Coefficients for Country-Election Populism range from 1.253 (dropping Hungary) 
* to 1.698 (dropping Hong Kong), all significant


* 2nd Paragraph of Results: Regional effects?
use "DGH_populism_polar.dta", clear
* including the UN Statistic's subregion as a fixed effect
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female i.UNRegion if good || countryyear:
* main populism results hold; country ideologial polarization gets weaker. unemployment and GINI go away.

* As a random effect (with countryelection as a nested random effect)
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || UNRegion: || countryyear:
* main results hold


* fn 19: code Proportional Representation, Mixed, and Majoritarian as separate variables
use "DGH_populism_polar.dta", clear
mixed new_polar_w dvy_par_pop dev_mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PRonly mixed female if good || countryyear: 
test PRonly mixed
* separate PR and mixed electoral system neither individually nor jointly statistically significant


* fn 21: interactive variable positive with the other 2 dependent variables
use "DGH_populism_polar.dta", clear
* unweighted distance
mixed new_polar_uw c.dvyuws_par_pop##c.dev_mnyuws_par_pop dvyuws_ideo_polar_m mnyuws_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good_uws || countryyear: 
* weighted variation
mixed a_polar c.dvy_par_pop##c.dev_mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good || countryyear: 


* fn 22: Expert/collaborator ratings of populism in CSES: In appendix Table A-2.


* Second Paragraph of Populism and the Radical Right
use "DGH_populism_polar.dta", clear
* Model 1 of Table 1 for Europe and Non-Europe
* all of Europe
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good & (UNRegion == 39 | UNRegion == 151 | UNRegion == 154 | UNRegion == 155) || countryyear:
* both populism variables +, sig

* everywhere but Europe
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good & UNRegion != 39 & UNRegion != 151 & UNRegion != 154 & UNRegion != 155 || countryyear:
* both populism variables +, sig

* Model 1 of Table 1 for right-populist and left-populist 
* right populist regions: Northern America, Eastern Europe, Northern Europe, Western Europe
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good & (UNRegion == 21 | UNRegion == 151 | UNRegion == 154 | UNRegion == 155) || countryyear:
* both populism variables +, sig

* left populist regions: South America, Central America, Southern Europe
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing close_noask unemploy gini PR1 female if good & (UNRegion == 5 | UNRegion == 13 | UNRegion == 39) || countryyear:
* individual-party populism + sig, country-election populism 0


* 2 paragraphs before Discussion/Conclusion and fn 26: Tables for Figure 5 in Appendix code



*
* Appendix
*


* Figure A-1
* Scatterplot of Affective Polarization (Weighted Distance) 
* and Individual Party Populism with a lowess curve
* Observations with negative Affective Polarization omitted.
* This takes a long time to run (lowess on 155K)
use "DGH_populism_polar.dta", clear
lowess new_polar_w party_pop if good & new_polar_w > -1e-04, ///
	msize(vtiny) ///
	title("") ///
	xtitle("Individual Party Populism") ///
	ytitle("Affective Polarization (Weighted Distance)") ///
	note("lowess prediction") ///
	graphregion(color(white))
graph save "Figure_A1", replace
graph export "Figure_A1.pdf", replace
* this is a large file, reduce resolution for manuscript


* Figure A-2
* Country-Election Populism for included country-elections.
* Ordered by average across elections
use "DGH_populism_polar.dta", clear
egen mny_par_pop_mean = mean(mny_par_pop) if pickone & good, by(country_name)
sort mny_par_pop_mean
egen pickcountry = tag(mny_par_pop_mean country_name)
keep if pickcountry
gen mny_par_pop_mean_rank = _n 
keep countrynum mny_par_pop_mean_rank
save "countryrankpopulism.dta", replace
use "DGH_populism_polar.dta", clear
preserve
capture drop _merge
keep if pickone & good
sort countrynum
merge m:1 countrynum using "countryrankpopulism.dta"
sort countrynum mny_par_pop
by countrynum (mny_par_pop): replace country_name = "" if _n != _N
scatter mny_par_pop_mean_rank mny_par_pop, ///
	msize(small) ///
	mcolor(black) ///
	mlabel(country_name) ///
	mlabsize(vsmall) ///
	mlabcolor(gs8) ///
	ytitle("") ///
	yscale(range(0 52)) ///
	ylabel(, noticks nolabels nogrid) ///
	xscale(range(.1 .87)) ///
	xlabel(.1(.1).8, labsize(small) nogrid) ///
	xtitle("Country-Election Populism", size(small)) ///
	ysize(8.5) xsize(6.5) ///
	graphregion(color(white)) ///
	plotregion(lcolor(black))
graph save "Figure_A2", replace
graph export "Figure_A2.pdf", replace
restore


* Figure A-3
* Scatterplot of Country-Election Populism and Country-Election Affective
* Polarization (Weighed Distance) with a linear regression line
use "DGH_populism_polar.dta", clear
twoway (scatter mny_new_polar_w mny_par_pop if pickone, mcolor(dknavy)) ///
	(lfit mny_new_polar_w mny_par_pop if pickone), ///
	legend(off) ///
	title("") ///
	xtitle("Country-Election Populism") ///
	ytitle("Country-Election Affective Polarization") ///
	xlabel(, nogrid) ///
	ylabel(, nogrid) ///
	graphregion(color(white))
graph save "Figure_A3", replace
graph export "Figure_A3.pdf", replace


* Figure A-4
* Proportion Stating a Close or Closer Party (for included country-elections)
* Ordered by the maximum proportion for a country among its included elections
use "DGH_populism_polar.dta", clear
egen nonind_coun_max = max(nonind_elec_mean) if pickone & good, by(country_name)
egen pickcoun = tag(nonind_coun_max country_name)
tab pickcoun, m
sum nonind_coun_max if pickcoun
sort nonind_coun_max
keep if pickcoun
gen nonind_coun_max_rank = _n 
keep countrynum nonind_coun_max_rank
save "countryranknonind.dta", replace
use "DGH_populism_polar.dta", clear
preserve
capture drop _merge
keep if pickone & good
sort countrynum
merge m:1 countrynum using "countryranknonind.dta"
sort countrynum nonind_elec_mean
by countrynum (nonind_elec_mean): replace country_name = "" if _n != _N
scatter nonind_coun_max_rank nonind_elec_mean, ///
	msize(small) ///
	mcolor(black) ///
	mlabel(country_name) ///
	mlabsize(vsmall) ///
	mlabcolor(gs8) ///
	ytitle("") ///
	yscale(range(0 52)) ///
	ylabel(, noticks nolabels nogrid) ///
	xscale(range(0 1.3)) ///
	xlabel(0(.2)1, labsize(small) nogrid) ///
	xtitle("Proportion Stating a Close or Closer Party", size(small)) ///
	ysize(8.5) xsize(6.5) ///
	graphregion(color(white)) ///
	plotregion(lcolor(black))
graph save "Figure_A4", replace
graph export "Figure_A4.pdf", replace
restore


* Table A-1
use "DGH_populism_polar.dta", clear
sum new_polar_w new_polar_uw a_polar party_pop dvy_par_pop mny_par_pop dev_mny_par_pop  ///
	ideo_polar_m dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ///
	ideo_polar_noask close_m ///
	close_missing close_noask unemploy gini PR1 female if good


* fn 3: Null Model ICC
use "DGH_populism_polar.dta", clear
mixed new_polar_w if good || countryyear:
estat icc


* Table A-2
* CSES respondent populism
use "DGH_populism_polar.dta", clear
mixed new_polar_w dvy_ind_pop_cses dev_mny_ind_pop_cses dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing unemploy gini PR1 female if good || countryyear:
local ll = e(ll)
local var_g = (exp(e(b)[1,14]))^2
local var_i = (exp(e(b)[1,15]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_cses_resp = group(country_name) if e(sample)
sum countrynum_cses_resp
local N_c = r(max)
outreg2 using apptable2, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster replace

* CSES respondent populism
* interaction
mixed new_polar_w c.dvy_ind_pop_cses##c.dev_mny_ind_pop_cses dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing unemploy gini PR1 female if good || countryyear:
* there is a negative interaction effect
local ll = e(ll)
local var_g = (exp(e(b)[1,15]))^2
local var_i = (exp(e(b)[1,16]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_cses_resp_inter = group(country_name) if e(sample)
sum countrynum_cses_resp_inter
local N_c = r(max)
outreg2 using apptable2, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append

* CSES collaborator populism
mixed new_polar_w dvy_ind_par_pop_cses dev_mny_par_pop_cses dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing close_m close_missing unemploy gini PR1 female if good || countryyear:
local ll = e(ll)
local var_g = (exp(e(b)[1,13]))^2
local var_i = (exp(e(b)[1,14]))^2
estat ic
local AIC = r(S)[1,5]
local BIC = r(S)[1,6]
estat icc
local icc = r(icc2)
egen countrynum_cses_coll = group(country_name) if e(sample)
sum countrynum_cses_coll
local N_c = r(max)
outreg2 using apptable2, addstat(Number of Countries, `N_c', Variance of Elections, `var_g', Variance of Individuals, `var_i', ICC, `icc', Log-likelihood, `ll', AIC, `AIC', BIC, `BIC') 2aster append text word excel


* Figure A-5
* substantive effects of CSES respondent populism model
use "DGH_populism_polar.dta", clear
mixed new_polar_w dvy_ind_pop_cses mny_ind_pop_cses dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing ideo_polar_noask close_m close_missing unemploy gini PR1 female if good || countryyear:
gen good_cses = e(sample)
xtset countryyear
xtsum new_polar_w if good_cses
xtsum dvy_ind_pop_cses if good_cses
scalar dvy_ind_pop_cses_sd = r(sd)
xtsum mny_ind_pop_cses if good_cses
scalar mny_ind_pop_cses_sd = r(sd)
xtsum dvy_ideo_polar_m if good_cses
scalar dvy_ideo_polar_m_sd = r(sd)
xtsum mny_ideo_polar_m if good_cses 
scalar mny_ideo_polar_m_sd = r(sd)
xtsum close_m if good_cses 
scalar close_m_sd = r(sd)
xtsum gini if good_cses 
scalar gini_sd = r(sd)
xtsum unemploy if good_cses 
scalar unemploy_sd = r(sd)
xtsum dvy_ind_par_pop_cses if good_cses

* all the effects
nlcom ///
	(dvy_ind_pop_cses: 2*dvy_ind_pop_cses_sd*_b[dvy_ind_pop_cses]) ///
	(mny_ind_pop_cses: 2*mny_ind_pop_cses_sd*_b[mny_ind_pop_cses]) ///
	(close_m: 2*close_m_sd*_b[close_m]) ///
	(dvy_ideo_polar_m: 2*dvy_ideo_polar_m_sd*_b[dvy_ideo_polar_m]) ///
	(mny_ideo_polar_m: 2*mny_ideo_polar_m_sd*_b[mny_ideo_polar_m]) ///
	(unemploy: 2*unemploy_sd*_b[unemploy]) ///
	(female: _b[female]) ///
	(PR1: _b[PR1]) ///
	(gini: 2*gini_sd*_b[gini]) 	///	
	, post
parmest, format(estimate min95 max95 %8.4f) saving("substeffects_cses.dta", replace)

use "substeffects_cses.dta", clear
egen varnum = fill(10 9)
replace varnum = varnum - 1 in 3/L
twoway (rspike min95 max95 varnum, horizontal) ///
	(scatter varnum estimate, mcolor(dknavy) msize(vsmall)), ///
	ylabel(1 "Inequality" 2 "Proportional Representation" 3 "Female" 4 "Unemployment Rate" ///
	5 "Country-Election Ideological Polarization" 6 "Individual Ideological Polarization" ///
	7 "Individual Party Closeness" 8 " " 9 "Citizenry Populism" 10 "Individual Populism", angle(horizontal) labsize(medium)) ///
	title("") ///
	ytitle("") ///
	xline(0, lcolor(dknavy) lpattern(dot))  ///
	legend(off) ///
	ytick(1/7 9/10) ///
	xlabel(, format(%8.2f) nogrid) ///
	ylabel(, glpattern(solid)) ///
	graphregion(color(white))
graph save "Figure_A5", replace
graph export "Figure_A5.pdf", replace


* Correlation between CSES collaborators and V-Party Collaborators on populism ratings
use "DGH_populism_polar.dta", clear
corr party_pop ind_par_pop_cses if good & pickonepartyyear
* 0.68


* fn 8: fit of different populism measures using the same observations:
* V-Party, CSES collaborator, CSES respondent
use "DGH_populism_polar.dta", clear
* To compare models, need the same sample: Find the observations that are in all three models
quietly mixed new_polar_w dvy_ind_par_pop_cses mny_par_pop_cses dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing close_m close_missing unemploy gini PR1 female if good || countryyear:
gen good_cses_coll = e(sample)
quietly mixed new_polar_w dvy_ind_pop_cses mny_ind_pop_cses dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing close_m close_missing unemploy gini PR1 female if good || countryyear:
gen good_cses_resp = e(sample)
gen good_all3 = (good & good_cses_coll & good_cses_resp)
tab good_all3, m
* now run all 3 models on that sample
mixed new_polar_w dvy_par_pop mny_par_pop dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing close_m close_missing unemploy gini PR1 female if good_all3 || countryyear: 
estimates store comp_vparty
mixed new_polar_w dvy_ind_par_pop_cses mny_par_pop_cses dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing close_m close_missing unemploy gini PR1 female if good_all3 || countryyear:
estimates store comp_cses_coll
mixed new_polar_w dvy_ind_pop_cses mny_ind_pop_cses dvy_ideo_polar_m mny_ideo_polar_m ideo_polar_missing close_m close_missing unemploy gini PR1 female if good_all3 || countryyear:
estimates store comp_cses_resp
estimates stats comp_*
* CSES collaborator estimate of populism fits best, next is V-DEM populism, worst is respondent populism


* Code for Tables A-3(a), A-3(b), and A-3(c) under Figure 5 above 


log close

